#!/bin/bash
# Gitee项目上传助手脚本
# 此脚本帮助用户将MCP项目上传到Gitee仓库

# 文字颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # 无颜色

# 打印带颜色的信息
info() {
    echo -e "${BLUE}[信息] $1${NC}"
}

success() {
    echo -e "${GREEN}[成功] $1${NC}"
}

warning() {
    echo -e "${YELLOW}[警告] $1${NC}"
}

error() {
    echo -e "${RED}[错误] $1${NC}"
}

# 检查git是否安装
if ! command -v git &> /dev/null; then
    error "未检测到Git。请先安装Git: sudo apt-get install git 或 sudo yum install git"
    exit 1
fi

# 获取当前目录名称作为默认仓库名
DEFAULT_REPO_NAME="conversation-mcp"

# 欢迎信息
echo "================================================="
echo "           Gitee MCP项目上传助手"
echo "================================================="
echo "此脚本将帮助您将MCP项目上传到Gitee仓库"
echo

# 获取Gitee用户名
read -p "请输入您的Gitee用户名 [默认: chen-jiankang1]: " GITEE_USERNAME
GITEE_USERNAME=${GITEE_USERNAME:-"chen-jiankang1"}

# 获取仓库名称
read -p "请输入仓库名称 [默认: $DEFAULT_REPO_NAME]: " REPO_NAME
REPO_NAME=${REPO_NAME:-$DEFAULT_REPO_NAME}

# 确认仓库信息
echo
info "您将使用以下信息创建Gitee仓库连接:"
echo "  - Gitee用户名: $GITEE_USERNAME"
echo "  - 仓库名称: $REPO_NAME"
echo "  - 仓库地址: https://gitee.com/$GITEE_USERNAME/$REPO_NAME.git"
echo

read -p "信息正确吗? (y/n): " CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
    warning "操作已取消"
    exit 0
fi

# 创建.gitignore文件
info "创建.gitignore文件..."
cat > .gitignore << EOL
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# 虚拟环境
venv/
mcp_env/
ENV/

# 日志文件
*.log

# 数据库备份
*.db.bak

# 本地配置
.env
EOL
success ".gitignore文件创建成功"

# 检查是否已经是Git仓库
if [ ! -d ".git" ]; then
    info "初始化Git仓库..."
    git init
    if [ $? -ne 0 ]; then
        error "Git初始化失败"
        exit 1
    fi
    success "Git仓库初始化成功"
else
    info "检测到已存在的Git仓库"
fi

# 检查远程仓库
REMOTE_EXISTS=$(git remote -v | grep -c "origin")
if [ $REMOTE_EXISTS -gt 0 ]; then
    warning "检测到已存在的远程仓库'origin'"
    read -p "是否覆盖? (y/n): " OVERRIDE_REMOTE
    if [[ "$OVERRIDE_REMOTE" =~ ^[Yy]$ ]]; then
        git remote remove origin
        success "已移除现有远程仓库"
    else
        warning "操作已取消"
        exit 0
    fi
fi

# 添加远程仓库
info "添加Gitee远程仓库(使用访问令牌)..."
git remote add origin "https://oauth2:4d0a6d757feb81811c5094c7863ee57e@gitee.com/$GITEE_USERNAME/$REPO_NAME.git"
if [ $? -ne 0 ]; then
    error "添加远程仓库失败"
    exit 1
fi
success "远程仓库添加成功"

# 添加所有文件（排除.gitignore中的文件）
info "添加所有文件到暂存区..."
git add .
if [ $? -ne 0 ]; then
    error "添加文件失败"
    exit 1
fi
success "文件添加成功"

# 提交更改
read -p "请输入提交信息 [默认: MCP项目初始提交]: " COMMIT_MSG
COMMIT_MSG=${COMMIT_MSG:-"MCP项目初始提交"}

info "提交更改..."
git commit -m "$COMMIT_MSG"
if [ $? -ne 0 ]; then
    error "提交更改失败"
    exit 1
fi
success "更改提交成功"

# 获取当前分支名
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ -z "$CURRENT_BRANCH" ]; then
    CURRENT_BRANCH="master"
fi

# 推送到Gitee
info "准备推送到Gitee ($CURRENT_BRANCH 分支)..."
echo "使用访问令牌进行认证，无需输入用户名和密码"
echo "重要: 请确保您已在Gitee上创建了名为 $REPO_NAME 的仓库"

read -p "您是否已在Gitee上创建了名为 $REPO_NAME 的仓库? (y/n): " REPO_CREATED
if [[ ! "$REPO_CREATED" =~ ^[Yy]$ ]]; then
    warning "请先在Gitee上创建仓库，步骤如下:"
    echo "1. 登录Gitee: https://gitee.com/login"
    echo "2. 点击右上角的'+' > '新建仓库'"
    echo "3. 仓库名填写: $REPO_NAME"
    echo "4. 选择可见性（公开或私有）"
    echo "5. 不要勾选'使用Readme文件初始化这个仓库'"
    echo "6. 点击'创建'"
    echo
    read -p "仓库创建完成后，按Enter键继续..." 
fi

read -p "是否继续推送? (y/n): " CONTINUE_PUSH
if [[ "$CONTINUE_PUSH" =~ ^[Yy]$ ]]; then
    info "正在使用访问令牌推送到Gitee..."
    git push -u origin "$CURRENT_BRANCH"
    if [ $? -ne 0 ]; then
        error "推送失败。请检查:"
        echo "  1. Gitee仓库是否已创建 (https://gitee.com/$GITEE_USERNAME/$REPO_NAME)"
        echo "  2. 您的用户名和密码是否正确"
        echo "  3. 远程仓库是否已有内容 (可能需要先git pull)"
        exit 1
    fi
    success "推送成功!"
    echo
    echo "您的MCP项目已成功上传到Gitee!"
    echo "仓库地址: https://gitee.com/$GITEE_USERNAME/$REPO_NAME"
else
    warning "推送操作已取消"
fi